<template>
	<om-addpage :busCode="busCode" style="padding-top: 35px;" menuName="工作计划调整申请表" @formSubmit="formSubmit" :showHistoryBtn="true">
		<!-- <uni-list>
			<uni-list-item :showArrow="!checkedit">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="制表岗位"></om-label></view>
					<view v-if="checkedit" class="tui-right tui-col-8">{{ formdata.zposName }}</view>
					<view v-else class="tui-right tui-col-8">
						<picker mode="selector" @change="zposFunc" :range="zposList" :value="zposIndex" range-key="posname">
							<view>{{ zposList.length ? zposList[zposIndex].posname : '请选择' }}</view>
						</picker>
					</view>
				</view>
			</uni-list-item>
			<uni-list-item :showArrow="!checkedit">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="计划年度" required></om-label></view>
					<view v-if="checkedit" class="tui-right tui-col-8">{{ formdata.year }}</view>
					<view v-else class="tui-right tui-col-8">
						<picker mode="selector" @change="yearFunc" :range="yearList" :value="yearIndex" range-key="name">
							<view>{{ ~yearIndex && yearList.length ? yearList[yearIndex].name : '请选择' }}</view>
						</picker>
					</view>
				</view>
			</uni-list-item>
			<uni-list-item :showArrow="!checkedit">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="计划月份" required></om-label></view>
					<view v-if="checkedit" class="tui-right tui-col-8">{{ formdata.monthName }}</view>
					<view v-else class="tui-right tui-col-8">
						<picker mode="selector" @change="monthFunc" :range="monthList" :value="monthIndex" range-key="dicname">
							<view>{{ ~monthIndex && monthList.length ? monthList[monthIndex].dicname : '请选择' }}</view>
						</picker>
					</view>
				</view>
			</uni-list-item>
		</uni-list> -->
		<om-white-space bgColor="#eee"></om-white-space>
		<om-sub title="明细">
			<view slot="extra">{{ dataList.length ? '点击项目可进行编辑' : '' }}</view>
			<uni-list v-for="(item, index) in dataList" :key="index">
				<uni-list-item :showArrow="true" @click="openData(index)">
					<view>{{ (item.orderNum || item.orderNum === 0 ? item.orderNum + ' | ' : '') + item.workName }}</view>
					<view class="text-gray text-sm padding-bottom-xs padding-top-xs">
						{{
								(item.deliveryName ? ' | ' + item.deliveryName : '') +
								(item.origPlanEnd ? ' | ' + item.origPlanEnd : '') +
								(item.adjustTypeName ? ' | ' + item.adjustTypeName : '') +
								(item.adjustPlanEnd ? ' | ' + item.adjustPlanEnd : '')+
								(item.adjustReasonName ? ' | ' + item.adjustReasonName : '')
						}}
					</view>
					<view class="text-gray text-sm">
						{{ item.remark }}
					</view>
				</uni-list-item>
			</uni-list>
			<view v-if="!checkedit" class="tui-flex align-center justify-center padding-top"><text class="lg text-green cuIcon-add" @click="openData()">增加明细</text></view>
		</om-sub>
		<om-white-space bgColor="#eee"></om-white-space>
	</om-addpage>
</template>

<script>
import { loadZpos,gw165 } from '../../../static/js/formOld.js';
import { getUrl,confirmModal } from '../../../static/js/common.js';
import { mapState } from 'pinia';
import {useLoginUserStore} from '@/stores/login-user.js';
const form = require('../../../static/js/formValidation.js');
export default {
	provide() {
		return {
			formPage: this
		};
	},
	data() {
		return {
			checkedit: false,
			formdata: {
				id: '',
				zuserId: '',
				zuserName: '',
				// zposId: '',
				// zposName: '',
				// year: '',
				// monthCode: '',
				// monthName: '',
				objStr: '' //明细
			},
			// zposIndex: 0,
			// zposList: [],
			// yearIndex: -1,
			// yearList: [],
			// monthIndex: -1,
			// monthList: [],
			dataList: [],
			dataCacheKey: 'obj-work-monthlyworkadjustapply-data-info-cache',
			menuName: '工作计划调整申请表',
			id: '',
			busCode: 'OBJ0510'
		};
	},
	computed: {
		...mapState(useLoginUserStore, ['userName', 'userId'])
	},
	watch: {
		dataList(newValue, oldValue) {
			let _that = this;
			if (newValue && newValue.length) {
				_that.formdata.objStr = JSON.stringify(newValue);
			} else {
				_that.formdata.objStr = '';
			}
		}
	},
	methods: {
		/**
		 * 初始化
		 */
		initLoad() {
			let _that = this;
			// //制表岗位
			// loadZpos(_that,function(){
			// 	gw165({
			// 		_that:_that,
			// 		posId:_that.formdata.zposId,
			// 		busCode:_that.busCode,
			// 	})
			// });
			// //计划年度
			// _that.loadYear();
			// //加载月度选项
			// this.loadMonth();
		},
		// /**
		//  * 制表岗位
		//  */
		// zposFunc(e) {
		// 	//清空明细数据
		// 	this.yearIndex = -1;
		// 	this.year = '';
		// 	this.monthIndex = -1;
		// 	this.monthCode = '';
		// 	this.monthName = '';
		// 	this.dataList = [];
		// 	if (e) {
		// 		if (this.zposList.length && e.target.value == -1) {
		// 			this.zposIndex = 0;
		// 		} else {
		// 			this.zposIndex = e.target.value;
		// 		}
		// 	}
		// 	if (~this.zposIndex && this.zposList.length) {
		// 		this.formdata.zposId = this.zposList[this.zposIndex].id;
		// 		this.formdata.zposName = this.zposList[this.zposIndex].posname;
		// 		if(e){
		// 			gw165({
		// 				_that:this,
		// 				posId:this.formdata.zposId,
		// 				busCode:this.busCode,
		// 			})
		// 		}
		// 	} else {
		// 		this.formdata.zposId = '';
		// 		this.formdata.zposName = '';
		// 	}
		// },

		// /**
		//  * 加载年度选项
		//  */
		// loadYear() {
		// 	let x = new Date().getFullYear();
		// 	this.yearList = [{ name: x }, { name: x + 1 }];
		// },

		// /**
		//  * 计划年度
		//  */
		// yearFunc(e) {
		// 	//清空明细数据
		// 	this.monthIndex = -1;
		// 	this.monthCode = '';
		// 	this.monthName = '';
		// 	this.dataList = [];
		// 	if (e) {
		// 		if (this.yearList.length && e.target.value == -1) {
		// 			this.yearIndex = 0;
		// 		} else {
		// 			this.yearIndex = e.target.value;
		// 		}
		// 	}
		// 	if (~this.yearIndex && this.yearList.length) {
		// 		this.formdata.year = this.yearList[this.yearIndex].name;
		// 	} else {
		// 		this.formdata.year = '';
		// 	}
		// },

		// /**
		//  * 加载月度选项
		//  */
		// loadMonth() {
		// 	let _that = this;
		// 	_that.om.request('dictZero/getListByCode', { code: '35803' }).then(res => {
		// 		if (res) {
		// 			_that.monthList = res;
		// 		} else {
		// 			_that.monthList = [];
		// 		}
		// 	});
		// },

		// /**
		//  * 月度
		//  */
		// monthFunc(e) {
		// 	//清空明细数据
		// 	this.dataList = [];
		// 	if (e) {
		// 		if (this.monthList.length && e.target.value == -1) {
		// 			this.monthIndex = 0;
		// 		} else {
		// 			this.monthIndex = e.target.value;
		// 		}
		// 	}
		// 	if (~this.monthIndex && this.monthList.length) {
		// 		this.formdata.monthCode = this.monthList[this.monthIndex].code;
		// 		this.formdata.monthName = this.monthList[this.monthIndex].dicname;
		// 	} else {
		// 		this.formdata.monthCode = '';
		// 		this.formdata.monthName = '';
		// 	}
		// },

		/**
		 * 编辑明细数据
		 * @param {Object} index
		 */
		openData(index) {
			uni.setStorage({
				key: this.dataCacheKey,
				data: this.dataList
			});
			// let zposId = this.formdata.zposId;
			// let busCode = this.busCode;
			// let year = this.formdata.year;
			// let monthCode = this.formdata.monthCode;
			// if (year && monthCode && zposId) {
				let param = {
					index:index,
					checkedit:this.checkedit ? true : '',
					cachename:this.dataCacheKey,
					// busCode:busCode,
					// zposId:zposId,
					// year:year,
					// monthCode:monthCode,
					editId:null,
				}
				//编辑明细
				let url = getUrl("/pages/form/obj-work-monthlyworkadjustapply/data-info",param);
				uni.navigateTo({
					url: url
				});
			// } else {
			// 	this.om.toast('请先选择制表岗位、计划年度、计划月份');
			// }
		},
		formSubmit() {
			let _that = this;
			_that.formdata.zuserId = this.userId;
			_that.formdata.zuserName = this.userName;
			//表单规则
			let rules = [
				// {
				// 	name: 'zposId',
				// 	rule: ['required'],
				// 	msg: ['请选择制表岗位']
				// },
				// {
				// 	name: 'year',
				// 	rule: ['required'],
				// 	msg: ['请选择计划年度']
				// },
				// {
				// 	name: 'monthCode',
				// 	rule: ['required'],
				// 	msg: ['请选择计划月份']
				// },
				{
					name: 'objStr',
					rule: ['required'],
					msg: ['请添加明细']
				}
			];
			var formdata = {};
			for (var i in _that.formdata) {
				if (_that.formdata[i]) {
					formdata[i] = _that.formdata[i];
				}
			}
			formdata.busCode = _that.busCode;
			let checkRes = form.validation(formdata, rules);
			
			if (!checkRes) {
				if (this.checkedit) {
					// _that.om.formSave('anyTimeReport/doReviewUpdate', formdata, _that, function(v){
					// 	uni.removeStorage({
					// 		key:_that.dataCacheKey
					// 	})
					// 	_that.om.getFormInfo(formdata.id, _that.busCode);
					// })
				} else {
					_that.om.formSave('tmonthlyWorkAdjustApply/addTmonthlyWorkAdjustApply', formdata, _that, function(v) {
						uni.removeStorage({
							key: _that.dataCacheKey
						});
						_that.om.getFormInfo(v, _that.busCode);
					});
				}
			} else {
				_that.om.toast(checkRes);
			}
		},
		loadData(id) {
			// let _that = this;
			// this.om.request('anyTimeReport/getValue', { fileId: id }).then(res => {
			// 	if (res.succeed) {
			// 		_that.formdata = res.value;
			// 		_that.dataList = res.value.dataList;
			// 		delete _that.formdata.dataList;
			// 		uni.setStorage({
			// 			key: _that.dataCacheKey,
			// 			data: _that.dataList
			// 		});
			// 	} else {
			// 		_that.om.toast(res.msg);
			// 	}
			// });
		}
	},
	onLoad: function(options) {
		let _that = this;
		_that.menuName = options.title || '工作计划调整申请表';
		uni.setNavigationBarTitle({
			title: _that.menuName
		});
		if (options.checkedit) {
			// this.checkedit = true
			// if(!options.fileId){
			// 	_that.om.toast("没有找到文件ID")
			// 	return
			// }
			// _that.loadData(options.fileId);
		} else {
			_that.initLoad();
		}
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		let _that = this;
		uni.getStorage({
			key: _that.dataCacheKey,
			success: function(res) {
				_that.dataList = res.data;
				uni.removeStorage({
					key: _that.dataCacheKey
				});
			}
		});
	}
};
</script>

<style></style>
